<template>
	<view>
		<Navbar></Navbar>
		<view class="u-m-25">
			<view class="u-font-34 u-font-bold u-m-b-20">剩余可提现{{drawData.end_money}}元</view>
			<view class="u-m-b-20" v-for="item in drawData.list" :key="item.id">
				<view class="u-card-box u-flex u-row-between u-p-t-30 u-p-b-30">
					<view class="u-font-32">¥{{item.money}}</view>
					<view class="u-tag-box u-font-26 u-back-puce u-p-t-8 u-p-b-8 u-p-l-20 u-p-r-20" @click="onWithDraw(item)">{{ item.is_grant == 1 ? '已提现' : '提现' }}</view>
				</view>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<Bottom :btnText="'返回'" @confirm="onBack"></Bottom>
		
		<!-- 通用提示框 -->
		<Modal :show="modal.show" :confirmText="modal.confirm" :modalContent="modal.content"
			:showCancel="modal.showCancel" @confirm="modalConfirm"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Bottom from '@/components/Bottom.vue'
	import Modal from '@/components/Modal.vue'
	import { teaWithdraw, teaDrawList } from '@/network/tea.js'
	export default{
		components:{
			Navbar,
			Bottom,
			Modal
		},
		data(){
			return{
				modal: {
					show: false,
					showCancel: true,
					confirm: '确定',
					content: ''
				},
				tea_id: 0,
				drawData: {}
			}
		},
		onLoad(options){
			this.tea_id = Number(options.tea_id)
			this.onGetDrawList()
		},
		methods:{
			async onGetDrawList(){
				const res = await teaDrawList({tea_id: this.tea_id})
				this.drawData = res.data
			},
			modalConfirm(){
				this.modal.show = false
				this.onGetDrawList()
			},
			async onWithDraw(val){
				if(val.is_grant == 1){
					return
				}else{
					const res = await teaWithdraw({
						tea_id: this.tea_id,
						log_id: val.id
					})
					switch(res.data.state){
						case 'WAIT_USER_CONFIRM':
						case 'TRANSFERING':
						    if (wx.canIUse('requestMerchantTransfer')) {
						    	wx.requestMerchantTransfer({
						    		mchId: res.data.mchid,
						    		appId: res.data.appid,
						    		package: res.data.package_info,
						    		success: (res) => {
										_this.onGetDrawList()
									},
						    		fail: (res) => {},
						    	});
						    } else {
								uni.showToast({
									title: "你的微信版本过低，请更新至最新版本。",
									icon: "none",
									duration: 3000
								})
						    }
							break;
						case 'ACCEPTED':
						    this.modal.content = '转账已受理'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
						case 'PROCESSING':
						    this.modal.content = '转账锁定资金中。如果一直停留在该状态，建议检查账户余额是否足够，如余额不足，可充值后再原单重试'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
						case 'SUCCESS':
						    this.modal.content = '转账成功'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
						case 'FAIL':
						    this.modal.content = '转账失败'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
						case 'CANCELING':
						    this.modal.content = '商户撤销请求受理成功，该笔转账正在撤销中'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
						case 'CANCELLED':
						    this.modal.content = '转账撤销完成'
							this.modal.showCancel = false
							this.modal.confirm = '确定'
							this.modal.show = true
							break;
					}
				}
			},
			onBack(){
				uni.navigateBack({
					delta: 1,
					success() {
						uni.$emit('update', {
							msg: '页面更新'
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>